package com.haonan.springboot_exercise.netty.client;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.stereotype.Component;

/**
 * netty 客户端初始化器
 * @author haonan
 * @date 2021/4/12 19:32
 **/
@EqualsAndHashCode(callSuper = true)
@Component
@Data
public class SmtpClientInitializer extends ChannelInitializer<SocketChannel> {
    private SmtpClientHandler smtpClientHandler;

    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        smtpClientHandler = new SmtpClientHandler();
        socketChannel.pipeline().addLast("decoder", new StringDecoder());
        socketChannel.pipeline().addLast("encoder", new StringEncoder());
        socketChannel.pipeline().addLast(smtpClientHandler);
    }

    public SmtpClientHandler getSmtpClientHandler(){
        return smtpClientHandler;
    }
}
